package com.xunge.service.impl;

import com.xunge.exception.DeleteException;
import com.xunge.mapper.DeptMapper;
import com.xunge.pojo.Dept;
import com.xunge.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findAll() {

        return deptMapper.findAll();

    }

    @Override
    public void deleteById(Integer id) {
        //
        Integer count = deptMapper.countById(id);
        if (count > 0){
            throw new DeleteException("该部门下有员工，不能删除");
        }else {
            deptMapper.deleteById(id);
        }

    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept getInfo(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public void updateById(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateById(dept);
    }
}
